//
//  File.swift
//  SwiftDemo
//
//  Created by John on 16/1/13.
//  Copyright © 2016年 SteamCat_343. All rights reserved.
//
import UIKit
let titleLabel: UILabel = UILabel(frame: CGRectMake(15, 120, UIScreen.mainScreen().bounds.width - 30, 50))
let inputField: UITextField = UITextField(frame: CGRectMake(15, 180, UIScreen.mainScreen().bounds.width - 30, 50))
var inputString = String()
class KeyboardViewController: UIViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        
        
        titleLabel.text = "输入法"
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.backgroundColor = UIColor(red: 131/255, green: 111/255, blue: 255/255, alpha: 1)
        self.view.addSubview(titleLabel)
        
        inputField.placeholder = "请在此输入工程"
        inputField.backgroundColor = UIColor(red: 171/255, green: 130/255, blue: 255/255, alpha: 1)
        self.view .addSubview(inputField)
        
        let inputBackView: UIView = UIView(frame: CGRectMake(0, 400, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 400))
        self.view.addSubview(inputBackView)
        
        let buttonTitles1 = ["区", "号楼", "层", "段", "轴线", "单元", "1", "2", "3", "删除"]
        let buttonTitles2 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "4","5", "6"]
        let buttonTitles3 = ["J", "K", "L", "M", "N", "O", "P", "Q", "R","7", "8", "9"]
        let buttonTitles4 = ["S", "T", "U", "V", "W", "X", "Y", "Z", "/", "~", "0", "-"]
        
        let row1 = createRowOfButtons(buttonTitles1)
        let row2 = createRowOfButtons(buttonTitles2)
        let row3 = createRowOfButtons(buttonTitles3)
        let row4 = createRowOfButtons(buttonTitles4)
        
        inputBackView.addSubview(row1)
        inputBackView.addSubview(row2)
        inputBackView.addSubview(row3)
        inputBackView.addSubview(row4)
        
        row1.translatesAutoresizingMaskIntoConstraints = false
        row2.translatesAutoresizingMaskIntoConstraints = false
        row3.translatesAutoresizingMaskIntoConstraints = false
        row4.translatesAutoresizingMaskIntoConstraints = false
 
        addConstraintsToInputView(inputBackView, rowViews: [row1, row2, row3, row4])
    }
    
    func createButtonWithTitle(title: String) -> UIButton {
        
        let button = UIButton(type: .System)
        button.frame = CGRectMake(0, 0, 20, 20)
        button.setTitle(title, forState: .Normal)
        button.sizeToFit()
        button.titleLabel!.font = UIFont.systemFontOfSize(15)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
        
        return button
    }
    
    func didTapButton(sender: UIButton) {
        inputString += (sender.titleLabel?.text)!
        inputField.text = inputString
        if sender.titleLabel?.text == "删除" {
            
        }
        titleLabel.text = sender.titleLabel?.text
    }
    
    func addIndividualButtonConstraints(buttons: [UIButton], rowView: UIView){
        
        for (index, button) in buttons.enumerate() {
            
            let topConstraint = NSLayoutConstraint(item: button, attribute: .Top, relatedBy: .LessThanOrEqual, toItem: rowView, attribute: .Top, multiplier: 1.0, constant: 1.0)
            let bottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .GreaterThanOrEqual, toItem: rowView, attribute: .Bottom, multiplier: 1.0, constant: -1.0)
            
            var rightConstraint : NSLayoutConstraint!
            
            if index == buttons.count - 1 {
                
                rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .GreaterThanOrEqual, toItem: rowView, attribute: .Right, multiplier: 1.0, constant: 0.0)
                
            } else {
                
                let nextButton = buttons[index+1]
                rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: nextButton, attribute: .Left, multiplier: 1.0, constant: -1.0)
            }
            
            
            var leftConstraint : NSLayoutConstraint!
            
            if index == 0 {
                
                leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .LessThanOrEqual, toItem: rowView, attribute: .Left, multiplier: 1.0, constant: 0.0)
                
            } else {
                
                let prevtButton = buttons[index-1]
                leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: prevtButton, attribute: .Right, multiplier: 1.0, constant: 1.0)
                
                let firstButton = buttons[0]
                let widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .Width, relatedBy: .Equal, toItem: button, attribute: .Width, multiplier: 1.0, constant: 0.0)
                
                widthConstraint.priority = 800.0
                rowView.addConstraint(widthConstraint)
            }
            
            rowView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
        }
    }
    
    func createRowOfButtons(buttonTitles: [NSString]) -> UIView {
        
        var buttons = [UIButton]()
        let keyboardRowView = UIView(frame: CGRectMake(0, 0, 320, 50))
        
        for buttonTitle in buttonTitles{
            
            let button = createButtonWithTitle(buttonTitle as String)
            buttons.append(button)
            keyboardRowView.addSubview(button)
        }
        
        addIndividualButtonConstraints(buttons, rowView: keyboardRowView)
        
        return keyboardRowView
    }
    
    func addConstraintsToInputView(inputView: UIView, rowViews: [UIView]){
        
        for (index, rowView) in rowViews.enumerate() {
            
            let rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .Right, relatedBy: .Equal, toItem: inputView, attribute: .Right, multiplier: 1.0, constant: 0.0)
            let leftConstraint = NSLayoutConstraint(item: rowView, attribute: .Left, relatedBy: .Equal, toItem: inputView, attribute: .Left, multiplier: 1.0, constant: 0.0)
            
            inputView.addConstraints([leftConstraint, rightSideConstraint])
            
            var topConstraint: NSLayoutConstraint
            
            if index == 0 {
                topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: inputView, attribute: .Top, multiplier: 1.0, constant: 0.0)
            } else {
                
                let prevRow = rowViews[index-1]
                topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: prevRow, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
                
                let firstRow = rowViews[0]
                let heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .Height, relatedBy: .Equal, toItem: rowView, attribute: .Height, multiplier: 1.0, constant: 0.0)
                
                heightConstraint.priority = 800.0
                inputView.addConstraint(heightConstraint)
            }
            inputView.addConstraint(topConstraint)
            
            var bottomConstraint: NSLayoutConstraint
            
            if index == (rowViews.count - 1) {
                bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: inputView, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
                
            } else {
                
                let nextRow = rowViews[index+1]
                bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: nextRow, attribute: .Top, multiplier: 1.0, constant: 0.0)
            }
            
            inputView.addConstraint(bottomConstraint)
        }
        
    }
}
